
function checkInput(event) {
    const target = event.target;
    let placeholder = target.getAttribute('placeholder');
    if (typeof target.value === 'undefined' || target.value === '') {
        if (placeholder.includes('验证码')) {
            let substr = placeholder.substr(-5, 5);
            target.setAttribute('placeholder', `请输入${substr}`);
        } else {
            placeholder = placeholder.replace('请输入', '');
            target.setAttribute('placeholder', `请输入${placeholder}`);
        }
        target.classList.add('login-input--red');
    }
}

function checkPhoneNumber(phoneNumber) {
    // 使用正则表达式检查手机号
    const phoneNumberRegex = /^1[3-9][0-9]{9}$/;
    return phoneNumberRegex.test(phoneNumber);
}

function startCountdown() {
    console.log('222222');
    let countdown = 60; // 初始倒计时时间
    // 获取验证码按钮
    const getVerificationCodeButton = document.getElementById("sendVerificationCode");
    const username = document.getElementById('username');
    if (checkPhoneNumber(username.value)) {
        $.ajax({
            url: "http://localhost:8080/smsC/sendSmsCode?phoneNumber=" + username.value+"&bizType="+"LOGIN_SMS_CODE",
            type: "post",
            async: false,
            cache: false,
            success: function (res) {
                if (res.code == 0) {
                    // 禁用按钮
                    getVerificationCodeButton.disabled = true;
                    // 更新按钮文本和倒计时
                    function updateButton() {
                        if (countdown <= 0) {
                            // 倒计时结束，启用按钮
                            getVerificationCodeButton.textContent = "获取验证码";
                            getVerificationCodeButton.disabled = false;
                        } else {
                            // 更新按钮文本和倒计时
                            getVerificationCodeButton.textContent = countdown + "秒后重新获取";
                            countdown--;
                            setTimeout(updateButton, 1000);
                        }
                    }
                    updateButton();
                } else {
                    cocoMessage.warning(res.msg,2000);
                }
            },
            error: function () {
                cocoMessage.error("验证码发送失败",2000);
            }
        })

    } else {
        cocoMessage.error("手机号格式错误",2000);
    }
}

function noPwdLogin() {
    //registerForm为表单的id，formdata可以获取表单的数据，比较方便
    console.log('111111');
    var form = new FormData(document.getElementById("login-form"));
    var code=$('#confirmCode').val();
    console.log(code);
    if(code===''||code==='null'){
        cocoMessage.error('请填写验证码',2000);
    }
    else {
        $.ajax({
            type: "POST",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/loginC/noPwdLogin",//要响应的url
            data: form,
            async: false,   //false为同步，true为异步
            processData: false,
            contentType : "application/json;charset=UTF-8",
            success: function (data) {
                if (data.code != 0) {
                    cocoMessage.error(data.msg);
                } else {
                    window.location.href = '/';
                }
            },
            error: function (result) {
                alert("出现异常");
                //刷新窗体
                window.location.href = window.location.href;
            }
        });
    }
}


